package com.zz.slick;

import java.util.ArrayList;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.state.StateBasedGame;

import com.zz.art.Art;
import com.zz.entity.Player;
import com.zz.entity.PlayerList;
import com.zz.tiles.Tile;

public class LevelSelect extends BasicGameState implements GameState {

	public String message = "";
	private ArrayList<Player> selectable;
	public static ArrayList<Player> players;
	private int selectedx;
	private int selectedy;
	private boolean overPlayer = false;
	private int mouseX;
	private int mouseY;
	
	public LevelSelect(int state) {

	}

	@Override
	public void mouseClicked(int arg0, int arg1, int arg2, int arg3) {

	}

	@Override
	public void mouseDragged(int arg0, int arg1, int arg2, int arg3) {

	}


	@Override
	public void mousePressed(int arg0, int arg1, int arg2) {
		
	}

	@Override
	public void mouseReleased(int arg0, int arg1, int arg2) {

	}

	@Override
	public void mouseWheelMoved(int arg0) {

	}

	@Override
	public void inputEnded() {

	}

	@Override
	public void inputStarted() {

	}

	@Override
	public boolean isAcceptingInput() {
		return false;
	}

	@Override
	public void setInput(Input arg0) {

	}

	@Override
	public void keyPressed(int arg0, char arg1) {

	}

	@Override
	public void keyReleased(int arg0, char arg1) {

	}

	@Override
	public void controllerButtonPressed(int arg0, int arg1) {

	}

	@Override
	public void controllerButtonReleased(int arg0, int arg1) {

	}

	@Override
	public void controllerDownPressed(int arg0) {

	}

	@Override
	public void controllerDownReleased(int arg0) {

	}

	@Override
	public void controllerLeftPressed(int arg0) {

	}

	@Override
	public void controllerLeftReleased(int arg0) {

	}

	@Override
	public void controllerRightPressed(int arg0) {

	}

	@Override
	public void controllerRightReleased(int arg0) {

	}

	@Override
	public void controllerUpPressed(int arg0) {

	}

	@Override
	public void controllerUpReleased(int arg0) {

	}

	@Override
	public void enter(GameContainer gc, StateBasedGame sbg) throws SlickException {

	}

	@Override
	public int getID() {
		return 1;
	}

	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		mouseMoved(0, 0, 0, 0);
		Art.loadImages();
		players = new ArrayList<Player>();
		selectable = new ArrayList<Player>();
		selectable.add(new Player(0));
		selectable.add(new Player(1));
		selectable.add(new Player(2));
		selectable.add(new Player(3));
		selectable.add(new Player(4));
		selectable.add(new Player(5));
	}

	private void addPlayer(Player p) {
		if(players.size() < 4) {
			players.add(p);
			selectable.remove(p);
		}
	}
	
	private void removePlayer(Player p) {
		players.remove(p);
		selectable.add(p);
	}
	
	@Override
	public void leave(GameContainer arg0, StateBasedGame arg1) throws SlickException {

	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		int i = 0;
		for(Player p : selectable) {
			g.drawImage(p.downAnim.getCurrentFrame(), i, 64);
			i += (Tile.size);
		}
		i = 0;
		for(Player p : players) {
			g.drawImage(p.downAnim.getCurrentFrame(), i, 128);
			i += (Tile.size);
		}
		
		if (overPlayer) {
			g.setColor(Color.black);
			g.drawRect(selectedx*Tile.size, selectedy*Tile.size, Tile.size-1, Tile.size-1);
			g.drawRect((selectedx*Tile.size)-2, (selectedy*Tile.size)-2, Tile.size+3, Tile.size+3);
			g.setColor(Color.white);
			g.drawRect((selectedx*Tile.size)-1, (selectedy*Tile.size)-1, Tile.size+2, Tile.size+2);
		}
		
		g.drawImage(Art.startButton, 10, 320);
		g.drawString(message, 0, 0);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		mouseX = Mouse.getX();
		mouseY = Options.getResY() - Mouse.getY();
		selectedx = mouseX / Tile.size;
		selectedy = mouseY / Tile.size;
		//message = selectedx + ", " + selectedy;
		/*for(Player p : players) {
			message.concat(p.name + ", ");
		}*/
		if(!players.isEmpty())
			message = players.get(0).name;
		int i;
		
		if ((selectedx >= 0) && (selectedx < 6) && ((selectedy == 2) || (selectedy == 4))) {
			overPlayer = true;
		} else {
			overPlayer = false;
		}
		
		if(Mouse.isButtonDown(0)) {
			
			if(mouseX >= 10 && mouseX <= 110 && mouseY >= 320 && mouseY <= 344) {
				PlayerList.list.addAll(players);
				sbg.enterState(2);
				
			//System.out.println(players.get(0).name);	
			}
			
			for(i = 0; i < selectable.size(); i++) {
				if( mouseX >= i * Tile.size && mouseX < i * Tile.size + Tile.size && mouseY >= 64 && mouseY <= 64 + Tile.size) {
					addPlayer(selectable.get(i));
				}
			}
		
			for(i = 0; i < players.size(); i++) {
				if( mouseX >= i * Tile.size && mouseX < i * Tile.size + Tile.size && mouseY >= 128 && mouseY <= 128 + Tile.size) {
					if(Mouse.isButtonDown(0)) {
						removePlayer(players.get(i));
					}
				}
			}
		}
	
		/*if(mouseX >= 100 && mouseX <= 200 && mouseY >= 100 && mouseY <= 124){
			if(Mouse.isButtonDown(0))
				sbg.enterState(1);
		}*/
		if(mouseX >= 100 && mouseX <= 200 && mouseY >= 550 && mouseY <= 574) {
			if(Mouse.isButtonDown(0))
				System.exit(0);
		}
	
	}

}
